<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty time modifier plugin
 *
 * Type:     modifier<br>
 * Name:     time<br>
 * Purpose:  Substr a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and
 *           appending the $etc string or inserting $etc into the middle.
 * @link http://smarty.php.net/manual/en/language.modifier.time.php
 *          time (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */
require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
//
function smarty_modifier_time($string = '', $type = '', $value = 0)
{
	if( $string == '' ) {
		return '';
	}
	if( $string == '0000-00-00 00:00:00' ) {
		return 'N/A';
	}
	return mxx_time_format($string, $type);
}

function mxx_time_format($string, $type = '', $value = 0) {
	if( strstr($type, 'ups') === false ) {
		$string = smarty_make_timestamp($string);
	}
	
	switch($type) {
		case 'full':
			$string = strftime("%Y-%m-%d %H:%M:%S", $string);
			break;
		case 'ymd':
			$string = strftime("%Y-%m-%d", $string);
			break;
		case 'ymds':
			$string = strftime("%Y-%M-%d", $string);
			break;
		case 'time':
			$string = strftime("%H:%M:%S", $string);
			break;
		case 'days':
			$string = smarty_make_timestamp($string) + 3600 * 24 * $value;
			$string = strftime("%Y-%m-%d", $string);
			break;
		case 'upsdate':
		case 'dateups':
			$string = substr($string, 0, 4) . '-' . substr($string, 4, 2) . '-' . substr($string, 6, 2);
			$string = smarty_make_timestamp($string);
			$string = strftime("%Y-%M-%d", $string);
			break;
		case 'upstime':
		case 'timeups':
			$string = substr($string, 0, 2) . ':' . substr($string, 2, 2) . ':' . substr($string, 4, 2);
			break;
		default:
			$string = strftime("%Y-%m-%d", $string);
			break;
	}
	
	return $string;
}
//
?>